"""
dic1 = {"name": "zzq",
        "age": "19"}
dic1["school"] = "jlu"
print(dic1)
dic2 = dict(name="zzx", age=15, school="slu")
print(dic2)
dic3 = dict([["name", "zz"], ["age", "18"], ["school", "xlu"]])
print(dic3)
dic4 = dict({"name": "zzq", "age": "19"}, school="jlu")
# 后面添加的部分可省略
print(dic4)
dic5 = dict(zip(["name", "age", "school"], ["zzq", "18", "jlu"]))
print(dic5)
print(dic1 == dic4)
"""
dic1 = dict.fromkeys("abc", 65)
print(dic1)
dic1.pop("a", "没有了")
# pop()删除函数，返回值是键对值
print(dic1)
dic1.popitem()
# 3.7版本前随机删除一个键值对，3.7之后删除最后一个，并返回键对
print(dic1)
del dic1
# 删除字典
dic1 = {"a": "65"}
dic1.clear()
print(dic1)
dic1["a"] = 2
dic1["b"] = 3
# clear()方法可以只清空字典的键值对
# update()函数可以一次更改多个键值对
dic1.update({"a": 1, "b": 2})
print(dic1)
# 查找除了用直接查找外，还可以使用get方法，其好处是找不到不会出现报错
print(dic1["a"])
print(dic1.get("a", "找不到"))
'''
使用keys静态方法可以获取键，values方法可以获取值，items方法可以获取键值对
'''
# 字典的拷贝
dic2 = dic1.copy()
# 字典长度的获取
length = len(dic1)
# 使用list()函数将字典转化成列表，默认只有键被存储到列表，如果需要值list(dic1.value())
dic3 = iter(dic1)
# 生成迭代器
'''
字典可以嵌套
'''
# 字典推导式
dic4 = {v: k for k, v in dic1.items()}
print(dic4)
dic5 = {v: k for k, v in dic1.items() if v < 5}
print(dic5)
